package org.syyo.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.syyo.admin.common.constant.PageConstant;
import org.syyo.admin.common.enums.ResultEnum;
import org.syyo.admin.common.exception.SysException;
import org.syyo.admin.domain.ResultVo;
import org.syyo.admin.domain.request.RoleReq;
import org.syyo.admin.domain.response.MenuResp;
import org.syyo.admin.domain.response.RoleResp;
import org.syyo.admin.mapper.RoleMapper;
import org.syyo.admin.mapper.RoleMenuMapper;
import org.syyo.admin.mapper.UserRoleMapper;
import org.syyo.admin.service.RoleService;
import org.syyo.admin.utils.CheckUtils;
import org.syyo.admin.utils.DateUtils;
import org.syyo.admin.utils.MyListUtils;
import org.syyo.admin.utils.ResultUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Auther: wangzhong
 * @Date: 2019/10/8 16:55
 * @Description:
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private PageConstant pageConstant;

    /**
     * 角色添加
     * @param req
     * @return
     */
    @Override
    @Transactional
    public ResultVo addRole(RoleReq req) {
        //检验参数是否为空
        CheckUtils.validate(req);
        //校验角色标识是否重复
        int count = roleMapper.checkRole(req.getRoleKey());
        if (count != 0){
            throw new SysException(ResultEnum.ROLE_EXIST.getCode(),ResultEnum.ROLE_EXIST.getMessage());
        }
        //添加创建时间
        Date date = DateUtils.getDate();
        req.setCreateDate(date);
        int i = roleMapper.addRole(req);
        if (i != 1){
            throw new SysException(ResultEnum.ROLE_ADD_FAIL.getCode(),ResultEnum.ROLE_ADD_FAIL.getMessage());
        }

        return ResultUtils.response(i);
    }

    /**
     * 删除角色
     * @param req
     * @return
     */
    @Override
    @Transactional
    public ResultVo delRole(RoleReq req) {
        Integer roleId = req.getRoleId();
        //校验角色id是否存在
        RoleResp roleResp = roleMapper.getRole(roleId);
        if (roleResp == null){
            throw new SysException(ResultEnum.ROLE_NOT_EXIST.getCode(),ResultEnum.ROLE_NOT_EXIST.getMessage());
        }
        //删除角色
        int i = roleMapper.delRole(roleId);
        if (i != 1){
            throw new SysException(ResultEnum.ROLE_DEL_FAIL.getCode(),ResultEnum.ROLE_DEL_FAIL.getMessage());
        }
        //删除对应的菜单
        roleMenuMapper.delRoleMenu(roleId);
        //删除对应的用户
        userRoleMapper.delUserRoleByRoleId(roleId);

        return ResultUtils.response(i);
    }

    /**
     * 修改角色
     * @param req
     * @return
     */
    @Override
    @Transactional
    public ResultVo editRole(RoleReq req) {
        //校验角色id是否存在
        RoleResp roleResp = roleMapper.getRole(req.getRoleId());
        if (roleResp == null){
            throw new SysException(ResultEnum.ROLE_NOT_EXIST.getCode(),ResultEnum.ROLE_NOT_EXIST.getMessage());
        }
        //添加更新时间
        Date date = DateUtils.getDate();
        req.setUpdateDate(date);
        int i = roleMapper.editRole(req);
        if (i != 1){
            throw new SysException(ResultEnum.ROLE_UPDATE_FAIL.getCode(),ResultEnum.ROLE_UPDATE_FAIL.getMessage());
        }
        return ResultUtils.response(i);
    }

    /**
     * 查询角色详情
     * @param roleId
     * @return
     */
    @Override
    public ResultVo getRole(Integer roleId) {
        RoleResp role = roleMapper.getRole(roleId);
        List<MenuResp> menus = role.getMenus();
        if(!MyListUtils.isEmpty(menus)){
            List<Integer> list = new ArrayList<>();
            for (MenuResp menu : menus) {
                //封装角色对应的权限，最低级类型
                if (menu != null && menu.getType() == 3){
                    list.add(menu.getMenuId());
                }
            }
            role.setMenuIds(list);
        }
        return ResultUtils.response(role);
    }

    /**
     * 角色列表
     * @param req
     * @return
     */
    @Override
    public ResultVo listRole(RoleReq req) {
        Integer pageNum = req.getPageNum();
        Integer pageSize = req.getPageSize();
        if(pageNum == null ){
            pageNum = pageConstant.getPageNum();
        }
        if(pageSize == null || pageSize > pageConstant.getPageRange() ){
            pageSize = pageConstant.getPageSize();
        }
        //分页处理
        PageHelper.startPage(pageNum,pageSize);
        List<RoleResp> roleResps = roleMapper.listRole(req);
        PageInfo<RoleResp> list = new PageInfo<>(roleResps);
        return ResultUtils.response(list);
    }

    /**
     * 角色授权
     * @param req
     * @return
     */
    @Override
    @Transactional
    public ResultVo auth(RoleReq req) {
        //校验角色id是否存在
        Integer roleId = req.getRoleId();
        RoleResp roleResp = roleMapper.getRole(roleId);
        if (roleResp == null){
            throw new SysException(ResultEnum.ROLE_NOT_EXIST.getCode(),ResultEnum.ROLE_NOT_EXIST.getMessage());
        }
        List<Integer> menuIds = req.getMenuIdList();
        if (menuIds != null && menuIds.size()>0){
            //删除该角色在中间表的权限信息
            roleMenuMapper.delRoleMenu(roleId);
            for (Integer menuId : menuIds) {
                //重新在中间表添加新的权限信息
                int i = roleMenuMapper.addRoleMenu(roleId, menuId);
                if (i != 1){
                    throw new SysException(ResultEnum.ROLE_AUTH_FAIL.getCode(),ResultEnum.ROLE_AUTH_FAIL.getMessage());
                }
            }
        }
        return ResultUtils.response(1);
    }
}
